﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsUnitDAL.cs
/// 作  者：SAM
/// 时  间：2006.11.10
/// 说  明：ClsUnitDAL类用于计量单位信息相关处理
/// </summary>
public class ClsUnitDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有的计量单位信息
    /// </summary>
    /// <param name="Co_ID">公司ID</param>
    /// <param name="User_ID">用户ID</param>
    /// <param name="Ds_ID">数据集ID</param>
    /// <returns>返回计量单位信息数据集</returns>
    public DataSet GetUnits()
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetUnit", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        //dat.SelectCommand.Parameters.Add("@co_id", Co_ID);
        //dat.SelectCommand.Parameters.Add("@user_id", User_ID);
        //dat.SelectCommand.Parameters.Add("@ds_id", Ds_ID);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 搜索计量单位信息
    /// </summary>
    /// <param name="Key">搜索关键字</param>
    /// <returns>返回搜索信息数据集</returns>
    public DataSet SearchUnits(string Key)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_SearchUnits", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@key", Key);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 作废计量单位信息
    /// </summary>
    /// <param>计量单位编号</param>
    /// <param name="Is_Visible">作废状态1为临时作废，－1为彻底作废</param>
    /// <returns>返回作废是否成功</returns>
    public Boolean DeleteUnit(string ID, int Is_Visible)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteUnit", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@Nt_id", SqlDbType.NVarChar, 20);
        com.Parameters["@Nt_id"].Value = ID;
        com.Parameters.Add("@Is_Visible", SqlDbType.Int, 1);
        com.Parameters["@Is_Visible"].Value = Is_Visible;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 确认计量单位信息
    /// </summary>
    /// <param>计量单位编号</param>
    /// <param name="AuditStat">确认状态</param>
    /// <param name="Auditor">确认人</param>
    /// <returns>返回确认是否成功</returns>
    public Boolean AuditStatUnit(string ID, int AuditStat, string Auditor)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AuditStatSetUnit", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@Nt_id", SqlDbType.NVarChar, 20);
        com.Parameters["@Nt_id"].Value = ID;
        com.Parameters.Add("@Is_audit", SqlDbType.Int);
        com.Parameters["@Is_audit"].Value = AuditStat;
        com.Parameters.Add("@Auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@Auditor"].Value = Auditor;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 增加计量单位信息
    /// </summary>
    /// <param>计量单位编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean AddUnit(string ID, string Name, string Remark, string Oper, bool nt_is_basicunit, int nt_basicpercent, bool nt_isaccountunit, string nt_basicunit)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddUnit", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@Nt_ID", SqlDbType.NVarChar, 20);
        com.Parameters["@Nt_ID"].Value = ID;
        com.Parameters.Add("@Nt_Name", SqlDbType.NVarChar, 20);
        com.Parameters["@Nt_Name"].Value = Name;
        //中间插入各表的数据和参数

        com.Parameters.Add("@Nt_Oper", SqlDbType.NVarChar, 60);
        com.Parameters["@Nt_Oper"].Value = Oper;
        com.Parameters.Add("@Remark", SqlDbType.NVarChar, 255);
        com.Parameters["@Remark"].Value = Remark;

        com.Parameters.Add("@nt_is_basicunit", SqlDbType.Bit);
        com.Parameters["@nt_is_basicunit"].Value = nt_is_basicunit;
        com.Parameters.Add("@nt_basicpercent", SqlDbType.Int);
        com.Parameters["@nt_basicpercent"].Value = nt_basicpercent;
        com.Parameters.Add("@nt_isaccountunit", SqlDbType.Bit);
        com.Parameters["@nt_isaccountunit"].Value = nt_isaccountunit;
        com.Parameters.Add("@nt_basicunit", SqlDbType.NVarChar, 60);
        com.Parameters["@nt_basicunit"].Value = nt_basicunit;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改计量单位信息
    /// </summary>
    /// <param>计量单位编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean UpDateUnit(string ID, string Name, string Remark, bool nt_is_basicunit, int nt_basicpercent, bool nt_isaccountunit, string nt_basicunit)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpDateUnit", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@Nt_ID", SqlDbType.NVarChar, 20);
        com.Parameters["@Nt_ID"].Value = ID;
        com.Parameters.Add("@Nt_Name", SqlDbType.NVarChar, 20);
        com.Parameters["@Nt_Name"].Value = Name;
        //中间插入各表的数据和参数

        com.Parameters.Add("@Remark", SqlDbType.NVarChar, 255);
        com.Parameters["@Remark"].Value = Remark;


        com.Parameters.Add("@nt_is_basicunit", SqlDbType.Bit);
        com.Parameters["@nt_is_basicunit"].Value = nt_is_basicunit;
        com.Parameters.Add("@nt_basicpercent", SqlDbType.Int);
        com.Parameters["@nt_basicpercent"].Value = nt_basicpercent;
        com.Parameters.Add("@nt_isaccountunit", SqlDbType.Bit);
        com.Parameters["@nt_isaccountunit"].Value = nt_isaccountunit;
        com.Parameters.Add("@nt_basicunit", SqlDbType.NVarChar, 60);
        com.Parameters["@nt_basicunit"].Value = nt_basicunit;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 用于查询窗口获取数据
    /// </summary>
    /// <returns></returns>
    public DataSet SearchUnitSets()
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_QueryWinGetDataByUnit", con);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

}
